<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;

class GoodsdetailsController extends Controller
{
    /** 
    *   商品详情页
    */
     public function getGoodsdetails(Request $request)
     {
        $gid = $request->input('id');

        $res = DB::table('project_goods')->where('id',$gid)->first();

        $row = DB::table('project_goods_details')->where('gid',$gid)->get();

        $result = DB::table("project_goods_comment")
        ->join("project_user","project_goods_comment.uid","=","project_user.id")
        ->join("project_goods","project_goods_comment.gid","=","project_goods.id")
        ->where("project_goods_comment.gid",$gid)
        ->select("project_goods_comment.*","project_user.picname","project_user.level","project_user.username","project_user.picname","project_goods.goodsname")
        ->get();
        // dd($result);
        return view('home/goodsdetails',['res'=>$res,'row'=>$row,'result'=>$result]);
     } 
 

     /**
     *  点赞存入数据库
     */
     public function getZan(Request $request)
     {
        $id = $_GET['id'];

        $arr['zan'] = $_GET['zv'];

        $res = DB::table("project_goods_comment")->where('id',$id)->update($arr);

        if($res) {
            echo "yes";
        } else {
            echo "no";
        }

        
     }

 
     /**
     *  商品详情页提交数据
     */
     public function postGoods(Request $request)
     {
       
     
        if(session()->get("username")) {

           echo "yes";
        } else {
            echo "no";
        }
        
     }

     /**
     *  商品详情页判断是否登陆
     */
     public function postLogin()
     {

      $username = $_POST['name'];  //用户名
      $password = $_POST['passpwd'];  //密码
      
      //查询数据库
      $res = DB::table('project_user')->where('username',$username)->first();

      //用户名是否存在
      if(!$res) return 3;

      //判断密码
      if(Hash::check($password,$res->password)) {

         //判断状态
         if($res->status == 0) return 4;

         //查询用户详情表
         $row = DB::table('project_user_details')->where('uid',$res->id)->first();

         // 判断比较是某天的第一次登陆  奖励金币
      
         $arr['gold'] = $row->gold;    //获取金币

         if((date('Ymd',time()) - date('Ymd',$row->lasttime))>=1){

              $arr['gold'] += 10;    //金币加上10
         }

         $arr['lasttime'] = time();   //最后一次登陆时间

         //修改详细资料
         $result = DB::table('project_user_details')->where('uid',$res->id)->update($arr);

         $row = DB::table("project_user")->where("id",$res->id)->first();

         if($result) {

            //成功以后将用户的信息存入session中
             session()->set("username",$row);

             return 7;    //跳转首页
         } else {

            return 6;     //登陆失败
         }

      }else {
         return 5;
      }

    }


    /**
    *   判断喜欢是否登陆
    */
     public function getXihuan(Request $request)
     {
        $id = $_GET['id'];

        $arr['status'] = 2;

        DB::table("project_goods")->where("id",$id)->update($arr);
     
        if(session()->get("username")) {

           echo "yes";

        } else {
            echo "no";
        }
        
     }



















}
